package com.message.queue.rabbit.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 主题交换机配置信息：只需要生产者加，如果单纯的消费者，可以不加
 * 功能最强大的交换器，能动态配置分发参数规则
 * Created by zhangbo on 2020/7/17.
 */
@Configuration
public class TopicRabbitConfig {


    public static final String MAN = "topic.man";

    public static final String WOMAN = "topic.woman";


    @Bean
    public Queue firstQueue() {
        return new Queue(MAN);
    }

    @Bean
    public Queue secondQueue() {
        return new Queue(WOMAN);
    }

    @Bean
    public TopicExchange exhange() {
        return new TopicExchange("topicExchange");
    }

    /*往topic.man中发送的队列才会分发到MAN的队列中，而往topic.woman中发送队列，无法分发到MAN中*/
    @Bean
    public Binding bindingExchangeMessage() {
        return BindingBuilder.bind(firstQueue()).to(exhange()).with(MAN);
    }

    /*本场景中，往topic.man和topic.woman中发送的消息，都会分发到队列WOMAN中*/
    @Bean
    public Binding bindingExchangeMessage2() {
        /*  星号（*）：用来表示一个单词 (必须出现的)，  井号（#）：用来表示任意数量（零个或多个）单词*/
        return BindingBuilder.bind(secondQueue()).to(exhange()).with("topic.#");
    }





}
